<?php
require 'include/global.php';
require './include/validate.php';
require './include/all_left.php';
include("FCKeditor/fckeditor.php") ;

require_once 'include/advisory_db.php';
$smarty->compile_check = true;

$smarty->display('header.tpl');
      
if($_GET[rid]){
	$rid = $_GET[rid] + 0;
	$sql = "SELECT a.*,b.jo_id,b.jo_uid,b.jo_title FROM pa_contact as a, pa_job as b WHERE a.re_aliasid = b.jo_id AND a.re_id = '".$rid."' AND ( b.jo_uid = '".$_SESSION['uid']."'  )";
	$contactInfo = $DB->fetch_one_array($sql);
	$rid = $_GET[rid];
}
if($_GET['emailId']){
	$emailId = $_GET['emailId'] + 0;
	$sql = "SELECT * FROM pa_email WHERE e_id = '".$emailId."' AND e_receivedrcid = '".$_SESSION['uid']."'";
	$emailInfo = $DB->fetch_one_array($sql);
	$emailUrl = "&emailId=".$emailId;
	if(empty($emailInfo)){
		$contactInfo = array();
	}
}


if(empty($contactInfo)){
	$smarty->compile_check = true;

   $err = "You are not authorized to view this page.";
   $smarty->assign("err",$err);
   $smarty->display("error.tpl");
   $smarty->display("allright.tpl");
   $smarty->display('footer.tpl');
   exit;
}

if($_POST['send'] == 1){
	if(trim($_POST['rc_subject']) == ''){
		$err .= "Subject is required<br>";
	}
	if (trim(strip_tags($_POST['rc_detail'])) == '')
	{
		$err .= "Message is required<br>";
	}
	if(	$err == ''){
		$sql = "SELECT rc_id,rc_email,rc_fname,rc_lname,rc_copname FROM pa_recuiter WHERE rc_id = '".$contactInfo['re_uid']."'";
		$toRecInfo = $DB->fetch_one_array($sql);

		$sql = "INSERT INTO pa_email (e_sentrcid, e_receivedrcid, e_sendertype,e_receivertype, e_joid, e_type, e_fromemail, e_fromname, e_toemail, e_toname, e_subject, e_content, e_contactid, e_created) VALUES (
			'".$_SESSION['uid']."',
			'".$contactInfo['re_uid']."',
			'recruiter',
			'recruiter',
			'".$contactInfo['jo_id']."',
			'1',
			'".$_SESSION['useremail']."',
			'".$_SESSION['rc_copname']." (".$_SESSION['realname'].")',
			'".$toRecInfo['rc_email']."',
			'".$toRecInfo['rc_copname']."(".$toRecInfo['rc_fname']." ".$toRecInfo['rc_lname'].")',
			'".$_POST['rc_subject']."',
			'".$_POST['rc_detail']."',
			'".$rid."',
			'".time()."'
		)";
		$DB->query($sql);
	
		$insertId = $DB->insert_id();

		//Update the application
		if($rid > 0){
			$sql = " UPDATE pa_contact SET re_isreplied = 1, re_emailId= '".$insertId."' WHERE re_id = '".$rid."' ";
			$DB->query($sql);
			if($emailId > 0){
				$sql = " UPDATE pa_email SET e_isreplied = 1 WHERE e_id = '".$emailId."' ";
				$DB->query($sql);
			}
		}

		//External Email
		$content    = $_POST['rc_detail'];
		$subject    = $_POST['rc_subject'];

		require_once("./include/class.phpmailer.php");

		$to  = $toRecInfo['rc_email'];

		$mail=new PHPMailer();

		$mail->IsMail();

		$mail->From     = $_SESSION['useremail'];
		$mail->FromName = $_SESSION['realname'];
		$mail->AddAddress($to,$toRecInfo['rc_fname']." ".$toRecInfo['rc_lname']);
		//$mail->AddAddress("terry800918@gmail.com");

		$mail->IsHTML(true);
		$mail->Subject = stripslashes($subject);
		$mail->Body	= stripslashes($content);
		$mail->CharSet = 'UTF-8';
		$mail->Send();

		if($_GET[emailId]>0){
			$err = "<h3 style=\"color:red\">Sent Successfully!</h3><meta http-equiv=\"refresh\" content=\"3;url=myemails.php?op_folder=1\">";
		}else{
			$err = "<h3 style=\"color:red\">Sent Successfully!</h3><meta http-equiv=\"refresh\" content=\"3;url=contactlist.php?&id=".$contactInfo['jo_id']."&tpp=posted\">";
		}
	}
}

ob_clean();
ob_start();

$datenow = date("m-d-Y");
$to = $contactInfo['re_fname'] ." ". $contactInfo['re_lname'];
$subject = "RE:". $contactInfo['re_subject'];

//FCKeditor
$oFCKeditor = new FCKeditor("rc_detail") ;
$oFCKeditor->BasePath	= "FCKeditor/" ;
$oFCKeditor->Value = '' ;
$oFCKeditor->Width = 450;
$oFCKeditor->Height= 300;
$oFCKeditor->ToolbarSet = "Basic";

if($emailId > 0){
	$oFCKeditor->Value = "<br><br><br>-----------------------------<br><b>On ".date("m/d/Y",$emailInfo['e_created']).", ".$contactInfo['re_fname']." ".$contactInfo['re_lname']." wrote: </b><br>".$emailInfo['e_content'];
}else{
	$oFCKeditor->Value = $emailInfo['e_content']."<br><br>-----------------------------<br><b>On ".date("m/d/Y",$contactInfo['re_regtime']).", ".$contactInfo['re_fname']." ".$contactInfo['re_lname']." wrote: </b><br>". $contactInfo['re_detail'];
}



echo '
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0" class="tableoutline" style="TABLE-LAYOUT: fixed" >
<tr><td>
<form action="contactreply.php?rid='.$rid.$emailUrl.'" method="POST" >
<table width="100%" border="0" align="center" cellpadding="4" cellspacing="1" class="table_style">
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
					Date:</td>
                       <td  width="70%" >'.$datenow.'</td>
                   </tr>
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
					To :</td>
                       <td  width="70%" >'.$to.'</td>
                   </tr>
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
					From Me :</td>
                       <td  width="70%" >'.$_SESSION['realname']."[".$_SESSION['useremail']."]".'</td>
                   </tr>
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
					Subject:</td>
                       <td  width="70%" ><input name="rc_subject" value="'.$subject.'" style="width:400px;backgroud-color:#FFFFFF;border:1px solid #999999;height:20px"></td>
                   </tr>
<tr class="firstalt" nowrap><td width="30%"  style="font-weight:bold">
				              Message:  </td><td width="70%" >'.$oFCKeditor->CreateHtml().'
							 </td>
					   </tr>
<tr class="tbhead">
		<td  align="center" colspan="2">
		<input type="hidden" name="send" value="1" />
		<input class="bginput button-secondary" type="submit"  value=" Send Message " />
';
if($emailId > 0){
	echo '	<input class="bginput button-secondary" type="button"  value=" Cancel " onclick="javascript:history.go(-1)"/>';
}else{
	echo  '	<input class="bginput button-secondary" type="button"  value=" Cancel " onclick="location.href=\'contactlist.php?&id='.$contactInfo['jo_id'].'&tpp=posted\'"/>';
}
echo '</td>
		</tr>
		</table>
</from></td></tr></table>
		';

$reg_data = ob_get_contents();
ob_end_clean();

if ($err != '')
{
	$smarty->assign("err", $err);
}
$smarty->assign("reg", $reg_data);

$title   = "Reply to Recruiter Message";
$smarty->assign('title',$title);
$smarty->display('allleft.tpl');

$smarty->display('contactreg.tpl');


$smarty->display('allright.tpl');
$smarty->display('footer.tpl');
?>
